﻿Public Class BuildPermissionInfo
    Public Const mscToken As String = "/"

#Region "Method"

    Public Shared Sub BuildActionTable(ByRef dtAction As DataSetPermission.ActionListDataTable)
        If dtAction IsNot Nothing Then
            dtAction.Clear()

            Dim oDictionary As Dictionary(Of Integer, String) = GetResourceByEnum(GetType(ActionEnum))
            If oDictionary IsNot Nothing AndAlso oDictionary.Count > 0 Then
                Dim drAction As DataSetPermission.ActionListRow = Nothing
                For Each i As Integer In oDictionary.Keys
                    drAction = dtAction.NewActionListRow
                    With drAction
                        .ActionID = i
                        .ActionName = oDictionary.Item(i)
                    End With

                    dtAction.AddActionListRow(drAction)
                Next

                dtAction.AcceptChanges()
            End If
        End If
    End Sub

    Public Shared Sub BuildPermissionGroupTable(ByRef dtGroup As DataSetPermission.PermissonGroupDataTable)
        If dtGroup IsNot Nothing Then
            dtGroup.Clear()

            Dim oDictionary As Dictionary(Of Integer, String) = GetResourceByEnum(GetType(PermissionGroupEnum))
            If oDictionary IsNot Nothing AndAlso oDictionary.Count > 0 Then
                Dim drGroup As DataSetPermission.PermissonGroupRow = Nothing
                For Each i As Integer In oDictionary.Keys
                    drGroup = dtGroup.NewPermissonGroupRow
                    With drGroup
                        .GroupID = i
                        .GroupName = oDictionary.Item(i)
                        Select Case CType(i, PermissionGroupEnum)
                            Case PermissionGroupEnum.Cash, PermissionGroupEnum.INInward, PermissionGroupEnum.OUTOutward
                                .ParentID = PermissionGroupEnum.Business
                            Case Else
                        End Select
                    End With

                    dtGroup.AddPermissonGroupRow(drGroup)
                Next
                dtGroup.AcceptChanges()
            End If
        End If
    End Sub

    Public Shared Sub BuildPermissionMappingTable(ByRef dsMapping As DataSetPermission)
        If dsMapping IsNot Nothing Then

            dsMapping.Action_Permission.Clear()
            'Dim drMapping As DataSetPermission.Action_PermissionRow = Nothing
            'If dsMapping.PermissonGroup.Count > 0 Then
            '    For Each dr As DataSetPermission.PermissonGroupRow In dsMapping.PermissonGroup.Rows

            '        Select Case CType(dr.GroupID, PermissionGroupEnum)
            '            Case PermissionGroupEnum.Business, PermissionGroupEnum.Cash, PermissionGroupEnum.Dictionary, PermissionGroupEnum.INInward, PermissionGroupEnum.OUTOutward
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.Use)
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.Edit)
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.Delete)
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.Print)
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.AddNew)
            '            Case PermissionGroupEnum.ImportExport, PermissionGroupEnum.Opening, PermissionGroupEnum.System
            '                AddMappingRow(dsMapping, dr.GroupID, 1, ActionEnum.Use)
            '        End Select
            '    Next
            'End If

            If dsMapping.PermissionList.Count > 0 Then
                For Each dr As DataSetPermission.PermissionListRow In dsMapping.PermissionList.Rows
                    Select Case CType(dr.PermissionID, PermissionEnum)

                        Case PermissionEnum.AccountingObject, PermissionEnum.Item, PermissionEnum.BankAccount, _
                            PermissionEnum.Unit, PermissionEnum.Employee, PermissionEnum.User, _
                            PermissionEnum.CashPayment, PermissionEnum.CashReceive, PermissionEnum.PurchaseInvoice, _
                            PermissionEnum.PurchaseOrder, PermissionEnum.PurchaseReturn, PermissionEnum.SaleInvoice, _
                            PermissionEnum.SaleReturn, _
                            PermissionEnum.ItemTransfer, PermissionEnum.Stock
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Use, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Edit, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Delete, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Print, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.AddNew, dr.MyCodeID)
                        Case PermissionEnum.AccountingObjectCategory, PermissionEnum.ItemCategory
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Use, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Edit, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Delete, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.AddNew, dr.MyCodeID)
                        Case PermissionEnum.AccountingObjectOpening, PermissionEnum.ItemOpening
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Use, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Edit, dr.MyCodeID)
                        Case PermissionEnum.ImportData, PermissionEnum.ExportData, PermissionEnum.SettingApplication, _
                            PermissionEnum.AuditLog, PermissionEnum.User, PermissionEnum.UserGroup, _
                            PermissionEnum.PermissionAssign
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Use, dr.MyCodeID)
                        Case PermissionEnum.Business, PermissionEnum.Cash, PermissionEnum.Dictionary, _
                            PermissionEnum.INInward, PermissionEnum.OUTOutward
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Use, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Edit, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Delete, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Print, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.AddNew, dr.MyCodeID)
                        Case PermissionEnum.ImportExport, PermissionEnum.Opening, PermissionEnum.System
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Use, dr.MyCodeID)
                        Case PermissionEnum.All
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Use, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Edit, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Delete, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.Print, dr.MyCodeID)
                            AddMappingRow(dsMapping, dr.PermissionID, 1, ActionEnum.AddNew, dr.MyCodeID)
                        Case Else
                            AddMappingRow(dsMapping, dr.PermissionID, 0, ActionEnum.Use, dr.MyCodeID)
                    End Select
                Next
            End If

            dsMapping.Action_Permission.AcceptChanges()
        End If
    End Sub

    Public Shared Sub AddMappingRow(ByVal dsMapping As DataSetPermission, ByVal ParamArray oParam As Object())
        Dim drMapping As DataSetPermission.Action_PermissionRow = dsMapping.Action_Permission.NewAction_PermissionRow
        With drMapping
            .MappingID = Guid.NewGuid
            .PermissionID = oParam(0)
            .PermissionType = oParam(1)
            .ActionID = oParam(2)
            .IsCheck = False
            .ActionName = dsMapping.ActionList.FindByActionID(.ActionID).ActionName
            .MyCodeID = oParam(3)
        End With
        dsMapping.Action_Permission.AddAction_PermissionRow(drMapping)
    End Sub

    Public Shared Sub BuildPermissionTable(ByRef dtPermission As DataSetPermission.PermissionListDataTable)
        If dtPermission IsNot Nothing Then
            dtPermission.Clear()

            Dim oDictionary As Dictionary(Of Integer, String) = GetResourceByEnum(GetType(PermissionEnum))
            If oDictionary IsNot Nothing AndAlso oDictionary.Count > 0 Then
                Dim drPermission As DataSetPermission.PermissionListRow = Nothing
                For Each i As Integer In oDictionary.Keys
                    drPermission = dtPermission.NewPermissionListRow
                    With drPermission
                        .PermissionID = i
                        .PermissionName = oDictionary.Item(i)

                        Select Case CType(i, PermissionEnum)
                            Case PermissionEnum.AccountingObject, PermissionEnum.BankAccount, PermissionEnum.Employee, _
                                PermissionEnum.Item, PermissionEnum.ItemCategory, PermissionEnum.Unit, _
                                PermissionEnum.AccountingObjectCategory, PermissionEnum.Stock
                                .ParentID = PermissionEnum.Dictionary
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, .ParentID, i)
                            Case PermissionEnum.ImportData, PermissionEnum.ExportData
                                .ParentID = PermissionEnum.ImportExport
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, .ParentID, i)
                            Case PermissionEnum.ItemOpening, PermissionEnum.AccountingObjectOpening
                                .ParentID = PermissionEnum.Opening
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, .ParentID, i)
                            Case PermissionEnum.CashPayment, PermissionEnum.CashReceive
                                .ParentID = PermissionEnum.Cash
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, PermissionEnum.Business, .ParentID, i)
                            Case PermissionEnum.PurchaseInvoice, PermissionEnum.PurchaseOrder, _
                                PermissionEnum.PurchaseReturn
                                .ParentID = PermissionEnum.INInward
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, PermissionEnum.Business, .ParentID, i)
                            Case PermissionEnum.SaleInvoice, PermissionEnum.SaleReturn, PermissionEnum.Order, _
                                PermissionEnum.ItemTransfer
                                .ParentID = PermissionEnum.OUTOutward
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, PermissionEnum.Business, .ParentID, i)
                            Case PermissionEnum.AuditLog, PermissionEnum.User, PermissionEnum.UserGroup, _
                                PermissionEnum.SettingApplication, PermissionEnum.PermissionAssign, _
                                PermissionEnum.UpgradeDatabase
                                .ParentID = PermissionEnum.System
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, .ParentID, i)
                            Case PermissionEnum.Cash, PermissionEnum.INInward, PermissionEnum.OUTOutward
                                .ParentID = PermissionEnum.Business
                                .MyCodeID = GetMYCodeID(PermissionEnum.All, .ParentID, i)
                            Case PermissionEnum.All
                                .MyCodeID = GetMYCodeID(i)
                            Case Else
                                .ParentID = PermissionEnum.All
                                .MyCodeID = GetMYCodeID(.ParentID, i)
                        End Select
                    End With

                    dtPermission.AddPermissionListRow(drPermission)
                Next

                dtPermission.AcceptChanges()
            End If
        End If
    End Sub

    Public Shared Function GetResourceByEnum(ByVal enumType As Type) As Dictionary(Of Integer, String)
        Dim oDictionary As New Dictionary(Of Integer, String)
        Dim oArrayValue As Array = [Enum].GetValues(enumType)

        If oArrayValue IsNot Nothing AndAlso oArrayValue.Length > 0 Then
            For i As Integer = 0 To oArrayValue.Length - 1
                oDictionary.Add(oArrayValue(i), _
                                My.Resources.ResourceManager.GetString(enumType.Name & "_" & oArrayValue(i).ToString))
            Next
        End If

        Return oDictionary
    End Function

    Public Shared Function GetMYCodeID(ByVal ParamArray oPara As Integer()) As String
        If oPara IsNot Nothing AndAlso oPara.Length > 0 Then
            Dim str As String = ""
            For i As Integer = 0 To oPara.Length - 1
                str += mscToken & oPara(i).ToString
            Next

            Return str
        End If
        Return Nothing
    End Function

#End Region
End Class
